#!/usr/bin/env bash

# grugly@sdf

source '../say.sh'

ch="$2"
url="$3"
usg='The command !url <url> prints the title in most cases.'
rgx='https?://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'

if [[ "$url" =~ $rgx ]]; then
    msg="$(
        wget -T 5 -qO - "$url" |
        awk -vRS="</title>" '/<title>/{gsub(/.*<title>/,"");print;exit}'
    )"
fi

[[ -z "$msg" ]] && msg="$usg"

say "$ch" "$msg"

